package com.ruoyi.wechat.api;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.degree.domain.BizSchools;
import com.ruoyi.degree.domain.WechatUserStars;
import com.ruoyi.degree.service.IBizSchoolsService;
import com.ruoyi.degree.service.IWechatUserStarsService;
import com.ruoyi.wechat.vo.StarsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/wxapi/userstars")
@Api(tags = "用户收藏API", description = "用户收藏相关接口")
public class WechatUserStarsApi {


    @Autowired
    private IBizSchoolsService bizSchoolsService;
    @Autowired
    private  IWechatUserStarsService wechatUserStarsService;
    @ApiOperation("收藏查找")
    @Anonymous
    @GetMapping("/selectUserStarsListByUserIdAndEduPlanId")
    public AjaxResult selectUserStarsListByUserIdAndEduPlanId(Long userId, Long eduPlanId) {
        List<StarsVO> starsVos = new ArrayList<>();
        //查询出所有用户的收藏信息
        WechatUserStars wechatUserStars = new WechatUserStars();
        wechatUserStars.setWechatUserId(userId);
        List<WechatUserStars> starsList = wechatUserStarsService.selectWechatUserStarsList(wechatUserStars);
        for (WechatUserStars wechatUserStars1 : starsList) {
            StarsVO starsVO = new StarsVO();
            //根据学校ID查询学校信息
            BizSchools bizSchools = bizSchoolsService.selectBizSchoolsById(wechatUserStars1.getObjectId());
            if (bizSchools != null) {
                starsVO.setWechatUserStarId(wechatUserStars1.getId());
                starsVO.setSchoolId(bizSchools.getId());
                starsVO.setName(bizSchools.getName());
                starsVO.setAvatar(bizSchools.getLogo());
                starsVO.setEduPlanId(bizSchools.getEduPlanId());
                starsVO.setPlaceId(bizSchools.getPlaceId());
                starsVO.setIntro(bizSchools.getIntro());
                starsVO.setCityName(bizSchools.getCityName());
                starsVO.setLogo(bizSchools.getLogo());
                if (eduPlanId == bizSchools.getEduPlanId()) {
                    starsVos.add(starsVO);
                }

            }

        }
        //把收藏信息里的大学信息查出来
        //
        return AjaxResult.success(starsVos);

    }

    @ApiOperation("删除收藏")
    @Anonymous
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return AjaxResult.success(wechatUserStarsService.deleteWechatUserStarsById(id));
    }

    //用户收藏
    //type =1 表示已收藏，type=0表示取消收藏
    @ApiOperation("用户收藏")
    @Anonymous
    @GetMapping("/addUserStars")
    public AjaxResult addUserStars(@RequestParam Long schoolId,@RequestParam Long wechatUserId,@RequestParam int type)
    {
        if(type==1){
            //先查询是否已经收藏
            WechatUserStars wechatUserStars = new WechatUserStars();
            wechatUserStars.setWechatUserId(wechatUserId);
            wechatUserStars.setObjectId(schoolId);
            List<WechatUserStars> list = wechatUserStarsService.selectWechatUserStarsList(wechatUserStars);
            if(list!=null && list.size()>0){
                return AjaxResult.error("您已收藏该学校，无需重复收藏");
            }
            WechatUserStars userStars = new WechatUserStars();
            userStars.setWechatUserId(wechatUserId);
            userStars.setObjectId(schoolId);
            return AjaxResult.success(wechatUserStarsService.insertWechatUserStars(userStars));
        }else{
            //根据用户ID和学校ID删除
            WechatUserStars userStars = new WechatUserStars();
            userStars.setWechatUserId(wechatUserId);
            userStars.setObjectId(schoolId);
            List<WechatUserStars> list = wechatUserStarsService.selectWechatUserStarsList(userStars);
            if(list!=null && list.size()>0){
                return AjaxResult.success(wechatUserStarsService.deleteWechatUserStarsById(list.get(0).getId()));
            }else{
                return AjaxResult.error("没有找到该收藏记录");
            }
        }

    }
}
